package com.qfedu.controller;

import com.qfedu.entity.Course;
import com.qfedu.entity.ResponseData;
import com.qfedu.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class CourseController {
    @Autowired
    private CourseService courseService;

    @RequestMapping("/getCourseList")
    public ResponseData getCourseList(int page,int limit){
        ResponseData<List<Course>> responseDataCourseList = courseService.getCourseList(page, limit);
        return responseDataCourseList;
    }

    @RequestMapping("/deleteCourseById")
    public ResponseData deleteCourseById(int id){
        ResponseData responseData=new ResponseData();
        int i = courseService.deleteCourseListById(id);
        if (i > 0) {
            responseData.setCode(ResponseData.SUCCESS_CODE);
            responseData.setMsg(ResponseData.SUCCESS_MSG);
        }else {
            responseData.setCode(ResponseData.FAIL_CODE);
            responseData.setMsg(ResponseData.FAIL_MSG);
        }
        return responseData;
    }
    @RequestMapping("/updateCourse")
    public ResponseData updateCourse(Course course){
        ResponseData responseData =new ResponseData();
        int i = courseService.updateCourse(course);
        if(i>0){
            responseData.setMsg(ResponseData.SUCCESS_MSG);
            responseData.setCode(ResponseData.SUCCESS_CODE);
        }else{
            responseData.setCode(ResponseData.FAIL_CODE);
            responseData.setMsg(ResponseData.FAIL_MSG);
        }
        return responseData;
    }
    @RequestMapping("/addCourse")
    public ResponseData addCourse(Course course){
        ResponseData responseData=new ResponseData();
        int i = courseService.addCourse(course);
        if(i>0){
            responseData.setCode(ResponseData.SUCCESS_CODE);
            responseData.setMsg(ResponseData.SUCCESS_MSG);
        }else{
            responseData.setCode(ResponseData.FAIL_CODE);
            responseData.setMsg(ResponseData.FAIL_MSG);
        }
        return responseData;
    }

}
